<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分" prop="grade">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签" prop="label">
        <el-input
          v-model="queryParams.label"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="commentList"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="用户昵称" prop="userId">
        <template slot-scope="scope">
            {{ getUserName(scope.row.userId) }}
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="评论内容" align="center" prop="content" />
      <el-table-column label="评分" align="center" prop="grade" />
      <el-table-column label="标签" align="center" prop="label" />
      <el-table-column label="图片" align="center" prop="image" >
        <template slot-scope="scope">
          <el-image
            :src="scope.row.image"
            :value="scope.row.image"
            :preview-src-list="[scope.row.image]"
            style="width: 100px; height: 50px">
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scancode:comment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

     <!-- 分页 -->
     <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="评论内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="评分" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-input v-model="form.label" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="图片">
          <imageUpload v-model="form.image"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listComment, getComment, delComment, addComment, updateComment, exportComment, dimSearch } from "@/api/scancode/comment";
import { listUser, getUser } from "@/api/scancode/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Comment",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 评论表格数据
      commentList: [],
      // 评论树选项
      commentOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderId: null,
        content: null,
        grade: null,
        label: null,
        image: null,
        parentId: null
      },
      total: 0,
      // 表单参数
      form: {
        userId: '',
        orderId: '',
        content: '',
        grade: '',
        label: '',
        image: '',
        parentId: ''
      },
      // 表单校验
      // rules: {
      //   orderId: [
      //     { required: true, message: "菜品名称不能为空", trigger: "blur" }
      //   ],
      //   classifyId: [
      //     { required: true, message: "菜品分类不能为空", trigger: "blur" }
      //   ],
      //   sprice: [
      //     { required: true, message: "菜品售价不能为空", trigger: "blur" }
      //   ],
      //   oprice: [
      //     { required: true, message: "菜品原价不能为空", trigger: "blur" }
      //   ]
      // },
      params:{
        orderId:0,
        label:'粉'
      },
      userMessage:[]
    };
  },
  created() {
    this.getList();
    //用户信息
    listUser().then( res => {
      this.userMessage = res.rows
    })
    //模糊查询
    // dimSearch(this.params).then( res => {
    //   console.log( res.list)
    // })
  },
  methods: {
    /** 查询评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = this.handleTree(response.rows, "id", "parentId");
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换评论数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.userId,
        children: node.children
      };
    },
	/** 查询评论下拉树结构 */
    getTreeselect() {
      listComment().then(response => {
        this.commehntOptions = [];
        const data = { id: 0, userId: '顶级节点', children: [] };
        data.children = this.handleTree(response.rows, "id", "parentId");
        this.commentOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderId: null,
        content: null,
        grade: null,
        label: null,
        image: null,
        createTime: null,
        parentId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // /** 新增按钮操作 */
    // handleAdd(row) {
    //   this.reset();
    //   this.getTreeselect();
    //   if (row != null && row.id) {
    //     this.form.parentId = row.id;
    //   } else {
    //     this.form.parentId = 0;
    //   }
    //   this.open = true;
    //   this.title = "添加评论";
    // },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   this.getTreeselect();
    //   if (row != null) {
    //     this.form.parentId = row.id;
    //   }
    //   getComment(row.id).then(response => {
    //     this.form = response.rows;
    //     this.open = true;
    //     this.title = "修改评论";
    //   });
    // },
    // /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateComment(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addComment(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm(`是否确认删除订单编号为${row.orderId}的订单评论`).then(function() {
        return delComment(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    getUserName(id){
      for (let i=0; i<this.userMessage.length; i++) {
        if (this.userMessage[i].id === id) {
          return this.userMessage[i].nickName
        }
      }
    }
  },

};
</script>
